package com.eb.buybuybuy.service.impl;

import com.eb.buybuybuy.annotation.Autowired;
import com.eb.buybuybuy.annotation.Component;
import com.eb.buybuybuy.annotation.Service;
import com.eb.buybuybuy.annotation.Transactional;
import com.eb.buybuybuy.bean.Cart;
import com.eb.buybuybuy.bean.CartGoods;
import com.eb.buybuybuy.bean.Goods;
import com.eb.buybuybuy.bean.UserCart;
import com.eb.buybuybuy.exception.GoodsQuantityNotEnoughException;
import com.eb.buybuybuy.mapper.CartMapper;
import com.eb.buybuybuy.mapper.GoodsMapper;
import com.eb.buybuybuy.service.CartService;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Service(proxyInterface = CartService.class)
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public UserCart list(HttpSession session) throws Exception {
        Integer userId = (Integer) session.getAttribute("userId");
        List<Cart> list = cartMapper.getByUserId(userId);

        // 把数据库中的购物车数据 转换成用户的购物车对象
        List<CartGoods> cartGoods = list.stream().map((item) -> {
            try {
                return new CartGoods(item, goodsMapper.getById(item.getGoodsId()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList());

        // 获取 总个数 总价格
        Integer countNumber = 0;
        BigDecimal countPrice = new BigDecimal(0);

        for (CartGoods cartGood : cartGoods) {
            countNumber += cartGood.getCart().getNumber();
            Goods goods = cartGood.getGoods();
            BigDecimal number = new BigDecimal(cartGood.getCart().getNumber());
            BigDecimal price = goods.getPrice();
            countPrice = countPrice.add(price.multiply(number));
        }

        return new UserCart(countNumber, countPrice, cartGoods);
    }

    @Override
    public void increase(HttpSession session, Integer goodsId) throws Exception {
        Integer userId = (Integer) session.getAttribute("userId");
        cartMapper.increase(userId, goodsId);
    }

    @Override
    public void decrease(HttpSession session, Integer goodsId) throws Exception {
        Integer userId = (Integer) session.getAttribute("userId");
        cartMapper.decrease(userId, goodsId);
    }

    @Override
    @Transactional
    public void buy(HttpSession session) throws Exception {
        Integer userId = (Integer) session.getAttribute("userId");
        List<Cart> cartList = cartMapper.getByUserId(userId);
        for (Cart cart : cartList) {
            Integer goodsId = cart.getGoodsId();
            Integer number = cart.getNumber();
            Goods goods = goodsMapper.getById(goodsId);
            // 商品库存不足
            if (number > goods.getQuantity()) {
                throw new GoodsQuantityNotEnoughException();
            } else {
                goodsMapper.decreaseNumber(goodsId, number);
            }
        }

        cartMapper.deleteByUserId(userId);
    }
}
